<template>
  <view>
    <u-navbar title="卡管理"></u-navbar>
    <!-- iconfont iconqia1 mui-pull-right  ng-scope -->
    <formz
      ref="formz"
      :inputs="inputs"
      :data="data"
      :disabled="!b_cardcz"
    ></formz>
    <fixedbtn v-if="b_cardcz">
      <u-button
        :loading="loading"
        type="error"
        @click="Cardsdelfn"
        :custom-style="btnstyle"
      >
        注销卡号
      </u-button>
      <u-button
        v-if="data.b_use == 1"
        :loading="loading"
        type="primary"
        @click="CardLossbtn"
        :custom-style="btnstyle"
      >
        卡挂失
      </u-button>
      <u-button
        v-if="data.b_use == 0"
        :loading="loading"
        type="primary"
        @click="CardRecbtn"
        :custom-style="btnstyle"
      >
        恢复正常
      </u-button>
      <u-button
        v-if="data.b_use == 0"
        :loading="loading"
        type="primary"
        @click="CardReplacebtn"
        :custom-style="btnstyle"
      >
        更换卡号
      </u-button>
      <u-button
        :loading="loading"
        type="primary"
        @click="Cardedtfn"
        :custom-style="btnstyle"
      >
        修改内容
      </u-button>
    </fixedbtn>
    <zmodal ref="zmodal"></zmodal>
    <cardreplace ref="cardreplace"></cardreplace>
  </view>
</template>

<script>
  import json from "../Cardsadd/cardjson.js";
  import cardreplace from "./components/CardReplace.vue";
  export default {
    components: {
      cardreplace,
    },
    data() {
      return {
        inputs: json.Cardedtjson,
        data: {},
        loading: false,
        query: {},
        btnstyle: {
          padding: "8rpx 20rpx",
          fontSize: "32rpx",
          flex: "1",
          margin: "0 10rpx",
        },
      };
    },
    onLoad(query) {
      this.query = query;
      this.init();
    },
    computed: {
      b_cardcz() {
        return true;
      },
    },
    methods: {
      async init() {
        // if (!this.query.cardid || !this.query.printid) {
        // 	this.$u.toast("错误")
        // 	return
        // }
        // await this.$store.dispatch("user/getUSearch");
        // await this.$store.dispatch("upw/getUpw")
        // let user = this.$store.state.user.USearch;
        // await this.$store.dispatch('station/getstationfn', user.pcs)
        let Cards = await this.$z.request("/Cards", {
          cardid: this.query.cardid,
          printid: this.query.printid,
        });
        let Card = await this.$z.request("/Card", {
          cardid: this.query.cardid,
          printid: this.query.printid,
        });
        Card[0].cards = Cards[0];
        this.data = Card[0];
        // console.log(Cards);
        // this.$nextTick(async () => {
        // 	this.$refs.formz.init()
        // })
      },
      gopage(url) {
        window.location.href = url;
      },
      async Cardsdelfn() {
        this.$refs.zmodal.init({
          content: "请确认是否注销此卡?",
          showcancel: true,
          confirm: async () => {
            if (this.data.je != "0") {
              this.$refs.zmodal.init({
                content: "卡内有余额是否清空余额后注销?",
                showcancel: true,
                confirm: async () => {
                  await this.$z.request("/Cardedt", {
                    ...this.data,
                    je: 0,
                  });
                  let par = await this.$z.request("/Carddel", {
                    printid: this.data.printid,
                  });
                  if (par[0].par == "succeed") {
                    this.$refs.zmodal.init({
                      content: "操作成功",
                      confirm: () => {
                        this.$z.back();
                      },
                    });
                  }
                },
              });
              return;
            }
            let par = await this.$z.request("/Carddel", {
              printid: this.data.printid,
            });
            if (par[0].par == "succeed") {
              this.$refs.zmodal.init({
                content: "操作成功",
                confirm: () => {
                  this.$z.back();
                },
              });
            }
          },
        });
      },
      CardLossbtn() {
        this.$refs.zmodal.init({
          content: "请确认是否挂失此卡?",
          showcancel: true,
          confirm: async () => {
            let par = await this.$z.request("/CardLoss", {
              printid: this.data.printid,
            });
            if (par[0].par == "succeed") {
              this.$refs.zmodal.init({
                content: "操作成功",
                confirm: () => {
                  this.init();
                  // this.$z.back()
                },
              });
            }
          },
        });
      },
      CardRecbtn() {
        this.$refs.zmodal.init({
          content: "请确认是否恢复正常?",
          showcancel: true,
          confirm: async () => {
            let par = await this.$z.request("/CardRec", {
              printid: this.data.printid,
            });
            if (par[0].par == "succeed") {
              this.$refs.zmodal.init({
                content: "操作成功",
                confirm: () => {
                  this.init();
                  // this.$z.back()
                },
              });
            }
          },
        });
      },
      CardReplacebtn() {
        this.$refs.cardreplace.init(this.data);
      },
      async del() {
        this.$refs.zmodal.init({
          content: "删除此数据",
          showcancel: true,
          confirm: async () => {
            let par = await this.$z.request("/Cardsedt", {
              ...this.data,
              pcs: 1,
              type: 0,
            });
            if (par[0].par == "succeed") {
              this.$refs.zmodal.init({
                content: "操作成功",
                confirm: () => {
                  this.$z.back();
                },
              });
            }
          },
        });
      },
      async Cardedtfn() {
        let data = await this.$refs.formz.validate();
        if (data.yxq == "") {
          delete data.yxq;
        }
        delete data.cards;
        if (!data.sje) {
          delete data.sje;
        }
        this.loading = true;
        let par = await this.$z.request("/Cardedt", data);
        if (par[0].par == "succeed") {
          this.$refs.zmodal.init({
            content: "操作成功",
            confirm: () => {
              this.$z.back();
            },
          });
        }
      },
    },
  };
</script>

<style lang="scss" scoped>
  .fixedbtnbox {
    width: 100%;
    padding: 0 30rpx;
  }
</style>
